package net.zjitc.day09;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * Created by Administrator on 2017/9/27.
 */
@WebServlet(name = "RequestServletTest",urlPatterns = "/requestTest")
public class RequestServletTest extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        //请求头里面的东西
        //User-Agent
        String value = request.getHeader("User-Agent");
        PrintWriter out = response.getWriter();
        out.write(value);
        out.write("<br/>-----------------------------<br/>");

        //获取所有头的信息输出到页面？？
        // Enumeration getHeaderNames()：获取所有请求头名称；
        Enumeration enumeration = request.getHeaderNames();
        while (enumeration.hasMoreElements()){
            //获得到名字
            String name = (String) enumeration.nextElement();
            //通过名字获得内容
            String values = request.getHeader(name);
            out.write(name+":"+values+"<br/>");

        }

        out.write("<br/>-----------------------------<br/>");
        /** http://localhost:8080/web/requestTest?user=aa&pass=aa
         *
        String getServerName()：获取服务器名，localhost；
        String getServerPort()：获取服务器端口号，8080；
        String getContextPath()：获取项目名，/web；
        String getServletPath()：获取Servlet路径，/requestTest；
        String getQueryString()：获取参数部分，即问号后面的部分：user=aa&pass=aa
        String getRequestURI()：获取请求URI，等于项目名+Servlet路径：/web/requestTest
        String getRequestURL()：获取请求URL，等于不包含参数的整个请求路径：http://localhost:8080/web/requestTest
         * */




    }
}
